﻿using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuaHangLego.Bussiness
{
    public class KhachHangBSS
    {
        public IList<HoaDonBan> hdbs { get; private set; }
        public int SoHD { get; private set; }
        public int SoHDGiao { get; private set; }
        public int SoHDChuaGiao { get; private set; }
        public int TongSP { get; private set; }
        public int TongTien { get; private set; }

        public void CreateData(int ID)
        {
            this.hdbs = HoaDonBanDA.Instance.GetAllByKhachHangID(ID);
            hdbs = hdbs.OrderByDescending(hd => hd.ThoiGian).ToList();
            SoHD = hdbs.Count;
            SoHDGiao = hdbs.Where(hd => hd.DaThanhToan == true).Count();
            SoHDChuaGiao = hdbs.Where(hd => hd.DaThanhToan == false).Count();
            int temp = 0;
            int temp2 = 0;
            foreach (HoaDon hd in hdbs)
            {
                var t = from ct in hd.DsCTHD
                        select ct.SanPham.GiaBan * ct.SoLuong;
                foreach (int value in t)
                    temp += value;
                var t2 = from ct in hd.DsCTHD
                         select ct.SoLuong;
                foreach (int value in t2)
                    temp2 += value;
            }
            TongSP = temp2;
            TongTien = temp;
        }

        public IList<KhachHang> GetKhachHangBy(string key)
        {
            var data = KhachHangDA.Instance.GetAll().Where(kh => (kh.HoTen.ToLower().Contains(key.ToLower())
                || kh.DiaChi.ToLower().Contains(key.ToLower())
                || kh.Email.ToLower().Contains(key.ToLower())
                || kh.DienThoai.ToLower().Contains(key.ToLower())));
            return data.ToList();
        }

        public IList<KhachHang> GetKhachHangAll()
        {
            return KhachHangDA.Instance.GetAll();
        }

    }
}
